package org.dragonnova.iot.business.cloud.transport.model;

import io.netty.buffer.ByteBuf;

public abstract class Packet {
	public static final int MAX_TCP_LENGTH = 1280;
	public static final byte HEADER = 0x7E;
	public static final byte[] END = { (byte) 0xDA, (byte) 0xBB };

	private byte header;
	protected byte[] body;
	protected byte[] end;
	/**
	 * 标识
	 */
	protected byte mt;

	public Packet() {
		this.header = HEADER;
		this.end = END;
	}

	public byte getMt() {
		return mt;
	}

	public void setMt(byte mt) {
		this.mt = mt;
	}

	/**
	 * 
	 * @description:根据消息标识解析包
	 * @author songxy DateTime 2017年3月1日 上午10:24:46
	 * @param mt
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public Packet decodePacket(byte mt, ByteBuf data) throws Exception {
		this.mt = mt;
		this.decode(data);
		return this;
	}

	public abstract void decode(ByteBuf data) throws Exception;

	public abstract ByteBuf encode();

	public byte[] getBody() {
		return this.body;
	}

	public byte getHeader() {
		return header;
	}

	protected abstract void releasePacket();
}
